package package06章_面向对象编程_基础._05oop.exer1_methodMore._02_args;

/**
 * @ClassName: StringConcatTest
 * @Author 86132
 * @Package package06章_面向对象编程_基础.oop05.exer1_methodMore._02_args
 * @Date 2024/2/11 22:49
 * @description:练习：可变形参的方法 练习：可变形参的方法
 * <p>
 * n个字符串进行拼接，每一个字符串之间使用某字符进行分割，如果没有传入字符串，那么返回空字符串""
 */
public class StringConcatTest {
    public static void main(String[] args) {

        StringConcatTest test = new StringConcatTest();
        String info = test.concat("-","hello","world");
        System.out.println(info);//hello-world

        System.out.println(test.concat("/", "hello"));

        System.out.println(test.concat("-"));

    }

    public String concat(String operator, String... strs) {
        String result = "";

        for (int i = 0; i < strs.length; i++) {
            if (i == 0) {
                result += strs[i];
            } else {
                result += (operator + strs[i]);
            }
        }
        return result;
    }
}
